package com.oscar.ehome.base;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.oscar.ehome.MainActivity;
import com.oscar.ehome.R;
import com.oscar.ehome.utils.util.CommonUtils;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/8/2
 * 状态栏通知广播
 */

public class NotifationReceiver extends BroadcastReceiver {

    private NotificationManager notificationManager;
    private Notification notification; //下载通知进度提示
    private NotificationCompat.Builder builder;
    String title = "";
    String content = "";
    public static int notifation = 1;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("GetuiSdkDemo", "onReceive: "+"事实上事实上 " );
        if (intent.getAction().equals(CommonUtils.notifation)) {
            title = intent.getStringExtra("title");
            content = intent.getStringExtra("content");
            if (!CommonUtils.isTopActivity(context)) {//在后台运行时
                initNotification(context);
                notifation = 2;
            } else {
                initNotifications(context);
            }
        }
    }

    //初始化通知
    public void initNotification(Context context) {
        Log.e("GetuiSdkDemo", "onReceive: "+"事实上事实上 " );
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= 26) {//判读版本是否在7.0以上

        } else {

            Intent intent = new Intent(context, MainActivity.class);
            intent.putExtra("intent", 1);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            PendingIntent pi = PendingIntent.getActivity(context, 0, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT);

            builder = new NotificationCompat.Builder(context);
            builder.setContentTitle(title) //设置通知标题
                    .setSmallIcon(R.mipmap.ehomelogo) //设置通知的小图标
                    .setLargeIcon(BitmapFactory
                            .decodeResource(context.getResources(),
                                    R.mipmap.ehomelogo)) //设置通知的大图标
                    .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) //设置通知的提醒方式： 呼吸灯
                    .setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级：最大
                    .setAutoCancel(false)//设置通知被点击一次是否自动取消
                    .setContentIntent(pi)
                    .setContentText(content);
            notification = builder.build();//构建通知对象
            notificationManager.notify(1, notification);
        }




    }

    //初始化通知
    public void initNotifications(Context context) {
        Log.e("GetuiSdkDemo", "onReceive: "+"事实上事实上 " );
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(title) //设置通知标题
                .setSmallIcon(R.mipmap.ehomelogo) //设置通知的小图标
                .setLargeIcon(BitmapFactory
                        .decodeResource(context.getResources(),
                                R.mipmap.ehomelogo)) //设置通知的大图标
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) //设置通知的提醒方式： 呼吸灯
                .setPriority(NotificationCompat.PRIORITY_MAX) //设置通知的优先级：最大
                .setAutoCancel(false)//设置通知被点击一次是否自动取消
                .setContentText(content);
        notification = builder.build();//构建通知对象
        notificationManager.notify(1, notification);
    }

}
